package com.ffsky.litepack.demo.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ffsky.litepack.demo.R;
import com.ffsky.litepack.demo.data.MyLinkSource;
import com.ffsky.litepack.demo.databinding.ActivityMainBinding;
import com.gitee.hljdrl.ffkit.FFKitApplication;
import com.gitee.hljdrl.ffkit.adapter.BaseRvAdapter;
import com.gitee.hljdrl.ffkit.adapter.FFKitAdapter;
import com.gitee.hljdrl.ffkit.app.FFSupporterActivity;
import com.gitee.hljdrl.ffkit.bean.FFKitLink;
import com.gitee.hljdrl.ffkit.builder.FFKitLinkSource;
import com.gitee.hljdrl.ffkit.databinding.FfkitActivtyBinding;
import com.gitee.hljdrl.ffkit.divider.FFKitGridDivider;
import com.gitee.hljdrl.ffkit.listener.OnExecuteListener;
import com.gitee.hljdrl.utils.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    final String TAG = "MainActivity";
    private ActivityMainBinding mBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView( mBinding.getRoot());
        initView();
        initData();
        initListener();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }
    public Activity getActivity() {
        return this;

    }

    public void initView() {
        Toolbar toolbar = findViewById(R.id.tool_bar);
        toolbar.setTitle("Home");
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);//这2行是设置返回按钮的
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        toolbar.getNavigationIcon().setTint(Color.WHITE);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initData() {
        Class<? extends FFKitLinkSource> cls = MyLinkSource.class;
        FFKitLinkSource homeDataSource = null;
        try {
            homeDataSource = cls.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        if (homeDataSource == null) {
            return;
        }
        //------------------------------------------
        boolean addInList = homeDataSource.isOverlayList();
        List<FFKitLink> list = homeDataSource.getList();

        List<FFKitLink> assemblyKitList = new ArrayList<>();
        if (!addInList) {
            assemblyKitList.add(new FFKitLink.Builder().setName("Toast").setOnExecuteListener(new OnExecuteListener() {
                @Override
                public void onExecute(Activity activity) {
                    Toast.makeText(activity, "onExecute", Toast.LENGTH_LONG).show();
                }
            }).build());
        }
        if (CollectionUtil.isEnable(list)) {
            assemblyKitList.addAll(list);
        }
        //
        FFKitAdapter adapter = new FFKitAdapter(this, assemblyKitList);
        RecyclerView.LayoutManager layoutManager = homeDataSource.getLayoutManager(this);
        if (layoutManager != null) {
            mBinding.rvList.setLayoutManager(layoutManager);
        } else {
            mBinding.rvList.setLayoutManager(new LinearLayoutManager(this));
        }
        mBinding.rvList.setAdapter(adapter);
        mBinding.rvList.addItemDecoration(new FFKitGridDivider(this));
        adapter.setOnItemClickListener(new BaseRvAdapter.OnItemClickListener<FFKitLink>() {
            @Override
            public void onItemClick(View v, int position, FFKitLink kitLink) {
                if (kitLink.activityClass != null) {
                    startActivity(getActivity(), kitLink.activityClass, kitLink.bundle);
                } else if (kitLink.onExecuteListener != null) {
                    try {
                        kitLink.onExecuteListener.onExecute(getActivity());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } else {
                    FFSupporterActivity.startLink(getActivity(), kitLink);
                }
            }
        });
    }

    public void initListener() {

    }

    public static void startActivity(Activity activity, Class<?> cls) {
        startActivity(activity, cls, null);
    }

    public static void startActivity(Activity activity, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivity(intent);
    }

    public static void startActivityForResult(Activity activity, Class<?> cls, int requestCode) {
        startActivityForResult(activity, cls, requestCode, null);
    }

    public static void startActivityForResult(Activity activity, Class<?> cls, int requestCode, Bundle bundle) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivityForResult(intent, requestCode);
    }
}